package org.huzhp.gateway.service.impl;

import org.huzhp.gateway.service.AbstractAuditLogService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * @Description: Rest服务类
 * @Author: huzp
 * @CreateDate: 2020/4/10 12:38
 * @Version: 1.0
 */
public class RestServiceAuditLogService extends AbstractAuditLogService {


    private static final String REQUEST_LOG_API = "/audit/requestLog";

    private String server;

    private RestTemplate restTemplate;

    public RestServiceAuditLogService(ExecutorService executor, String server, RestTemplate restTemplate) {
        super(executor);
        this.server = server;
        this.restTemplate = restTemplate;
    }

    @Override
    public void auditLog(Map<String, Object> param) {
        try {
            param.put("logTime",sdf.format(param.get("logTime")));
            MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                postParameters.add(entry.getKey(), entry.getValue());
            }
            restTemplate.postForObject(this.server + REQUEST_LOG_API, postParameters, Boolean.class);
        } catch (RestClientException e) {
            logger.error("请求日志省审计接口{}出错",REQUEST_LOG_API);
        }
    }


    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

}
